<?php
	require_once('/database/database.php');
	require_once('/database/DBConfig.php');

	class MemberListHandler {
		private  $m_db = null;
		
		public function __construct (Database $db) {
			$this->m_db = $db;
		}
		
		/**
		 * Gets an array of usernames
		 * @return array $ml
		 */
		public function GetMemberList () {
			$ml =  $this->SelectMemberList();			
			
			return $ml;
		}
		
		/**
		 * Selects array of usernames
		 * @return array
		 */
		public function SelectMemberList() {
			$sql = "SELECT Username FROM userid";
			$stmt = $this->m_db->Prepare($sql);
			
			return $this->m_db->Select($stmt);
		}
		
		/**
		 * Removes a member from db
		 * @param MemberListVIew $mlw
		 */
		public function RemoveMember(MemberListView $mlw) {
			$user = $mlw->GetRemoveUsername();
			
			if (isset($user)) {
				$this->RemoveMemberQuery($user);
			}
		}
		
		/**
		 * @param string $user
		 */
		public function RemoveMemberQuery ($user) {				
			$sql = "DELETE FROM userid WHERE Username = ?";
			$stmt = $this->m_db->Prepare($sql);
			$stmt->bind_param("s", $user);
	
			$this->m_db->RunInsertQuery($stmt);
		}
		
		/**
		 * @param MemberListView $mlw
		 * @return string Testdata
		 */
		public function Test(MemberListView $mlw) {
			if ($this->RemoveMember("ratt")) {
				echo "hittar inte användaren.";
				return false;
			}
			
			return true;
		}
	}
?>